package com.gnerv.cms.freemarker.tag;

import com.gnerv.cms.dto.ArticleDTO;
import com.gnerv.cms.freemarker.FreemarkerComponent;
import com.gnerv.cms.utils.FreemarkerUtils;
import freemarker.core.Environment;
import freemarker.template.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author Gnerv LiGen
 * @description
 * @data 2019/4/3
 */
@FreemarkerComponent("g_list")
public class ListTagDirective implements TemplateDirectiveModel {

    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
        DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);
        List objs = (List) FreemarkerUtils.getParamToList("collection", map);
        String item = FreemarkerUtils.getParamToString("item", map);
        for (Object obj : objs) {
            TemplateModel tm = builder.build().wrap(obj);
            environment.setVariable(item, tm);
            templateDirectiveBody.render(environment.getOut());
        }
    }

}
